package com.donica.wamstest.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import com.donica.wamstest.entity.AirfleetVO;
import com.donica.wamstest.entity.AirlineVO;
import com.donica.wamstest.entity.AirplaneVO;
import com.donica.wamstest.entity.CommonFindVO;
import com.donica.wamstest.entity.DeviceFindVO;
import com.donica.wamstest.entity.DeviceInfoFindVO;
import com.donica.wamstest.entity.DeviceInfoVO;
import com.donica.wamstest.entity.DeviceVO;
import com.donica.wamstest.entity.PageResult;
import com.donica.wamstest.entity.ReturnMessage;
import com.donica.wamstest.mapper.DeviceMapper;
import com.donica.wamstest.utils.CommonUtil;

@Service
public class DeviceManageService {

	@Autowired
	DeviceMapper deviceMapper;

	/*
	 * airfleet
	 */

	public long InsertAirfleet(List<AirfleetVO> volist) {
		for (AirfleetVO deviceVO : volist) {

			deviceVO.setId(CommonUtil.get32UUID());
			deviceVO.initNullItem();
		}
		return deviceMapper.InsertAirfleet(volist);
	}

	public long deleteAirfleet(List<AirfleetVO> volist) {
		return deviceMapper.deleteAirfleet(volist);
	}

	public ReturnMessage searchAirfleet(CommonFindVO vo) {
		ReturnMessage rtnMsg = new ReturnMessage();
		try {
			long total = deviceMapper.searchAirfleetCount(vo);
			List<AirfleetVO> result = deviceMapper.searchAirfleet(vo);
			PageResult pageResult = new PageResult();
			pageResult.setTotalCount(total);
			pageResult.setPageSize(vo.getPageSize());
			pageResult.setCurrPage(vo.getCurrPage());
			pageResult.setResult(result);
			rtnMsg.setResult(pageResult);
		} catch (Exception e) {
			rtnMsg.setCode(ReturnMessage.FAIL);
			rtnMsg.setMsg(e.getMessage());
		}

		return rtnMsg;
	}

	/*
	 * airplane
	 */

	public long InsertAirplane(List<AirplaneVO> volist) {
		for (AirplaneVO deviceVO : volist) {

			deviceVO.setId(CommonUtil.get32UUID());
			deviceVO.initNullItem();
		}
		return deviceMapper.InsertAirplane(volist);
	}

	public long UpdateAirplane(List<AirplaneVO> volist) {
		return deviceMapper.UpdateAirplane(volist);
	}

	public long deleteAirplane(List<AirplaneVO> volist) {
		return deviceMapper.deleteAirplane(volist);
	}

	public ReturnMessage searchAirplane(CommonFindVO vo) {
		ReturnMessage rtnMsg = new ReturnMessage();
		try {
			long total = deviceMapper.searchAirplaneCount(vo);
			List<AirplaneVO> result = deviceMapper.searchAirplane(vo);
			PageResult pageResult = new PageResult();
			pageResult.setTotalCount(total);
			pageResult.setPageSize(vo.getPageSize());
			pageResult.setCurrPage(vo.getCurrPage());
			pageResult.setResult(result);
			rtnMsg.setResult(pageResult);
		} catch (Exception e) {
			rtnMsg.setCode(ReturnMessage.FAIL);
			rtnMsg.setMsg(e.getMessage());
		}

		return rtnMsg;
	}

	public List<AirlineVO> searchAirlines(AirlineVO vo) {
		return deviceMapper.searchAirlines(vo);
	}

	public long InsertDevice(List<DeviceVO> volist) {
		for (DeviceVO deviceVO : volist) {

			deviceVO.setId(CommonUtil.get32UUID());
			deviceVO.initNullItem();
		}
		return deviceMapper.InsertDevice(volist);
	}

	public long UpdateDevice(List<DeviceVO> volist) {
		return deviceMapper.UpdateDevice(volist);
	}

	public long deleteDevice(List<DeviceVO> volist) {
		return deviceMapper.deleteDevice(volist);
	}

	public ReturnMessage searchDevice(DeviceFindVO vo) {
		ReturnMessage rtnMsg = new ReturnMessage();
		try {
			
			long total = deviceMapper.searchDeviceCount(vo);
			List<DeviceVO> result = deviceMapper.searchDevice(vo);
			PageResult pageResult = new PageResult();
			pageResult.setTotalCount(total);
			pageResult.setPageSize(vo.getPageSize());
			pageResult.setCurrPage(vo.getCurrPage());
			pageResult.setResult(result);
			rtnMsg.setResult(pageResult);
		} catch (Exception e) {
			rtnMsg.setCode(ReturnMessage.FAIL);
			rtnMsg.setMsg(e.getMessage());
		}

		return rtnMsg;
	}

	public long InsertDeviceInfo(List<DeviceInfoVO> volist) {
		for (DeviceInfoVO deviceInfoVO : volist) {

			deviceInfoVO.setId(CommonUtil.get32UUID());
			deviceInfoVO.initNullItem();
			deviceInfoVO.calGroupIndex();
		}
		return deviceMapper.InsertDeviceInfo(volist);
	}

	public long deleteDeviceInfo(List<DeviceInfoVO> volist) {
		return deviceMapper.deleteDeviceInfo(volist);
	}

	public ReturnMessage searchDeviceInfo(DeviceInfoFindVO vo) {
		ReturnMessage rtnMsg = new ReturnMessage();
		try {
			long total = deviceMapper.searchDeviceInfoCount(vo);
			List<DeviceInfoVO> result = deviceMapper.searchDeviceInfo(vo);
			PageResult pageResult = new PageResult();
			pageResult.setTotalCount(total);
			pageResult.setPageSize(vo.getPageSize());
			pageResult.setCurrPage(vo.getCurrPage());
			pageResult.setResult(result);
			rtnMsg.setResult(pageResult);
		} catch (Exception e) {
			rtnMsg.setCode(ReturnMessage.FAIL);
			rtnMsg.setMsg(e.getMessage());
		}

		return rtnMsg;
	}

	public long calDeviceOnline() {
		//300s online状态未更新，设置为离线
		return deviceMapper.calDeviceOnline();
	}

	//每60s重新计算设备在线状态
	@Scheduled(initialDelay = 1000L, fixedDelay = 60000L)
	public void calDeviceOnlineTimer() {
		System.out.println("calDeviceOnline 当前时间:" + new Date());

		calDeviceOnline();
	}
}
